//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Resource Application Block
//===============================================================================

using System;
using System.IO;
using System.Drawing;
using System.Globalization;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using EntLibContrib.Resource;
using EntLibContrib.Resource.Managers;

namespace EntLibContrib.Resource.Tests
{
	public static class TestDataResources
	{
		#region Fields
		private static Database database;
		private static string baseName = "Test";
		private static ResourceManagerFactory factory;
		private static ResourceManager resourceManager;
		#endregion
		
		#region properties
		public static Database Database
		{
			get { return database; }
		}

		public static string BaseName
		{
			get { return baseName; }
		}
		#endregion

		#region Public Static Methods
		public static void Create ()
		{
			DataResourceWriter dataResourceWriter;
			ResourceDataNode resourceDataNode;
			ResourceFileRef resourceFileRef;
			
			database = DatabaseFactory.CreateDatabase();
			factory = new ResourceManagerFactory(TestConfigurationSource.GenerateConfiguration());
			resourceManager = factory.Create("AssemblyResourceManager");
			
			resourceManager.CultureInfo = CultureInfo.InvariantCulture;
			dataResourceWriter = new DataResourceWriter(database, baseName, new CultureInfo(""));
			dataResourceWriter.AddResource("Ewdev", resourceManager["Ewdev"]);
			dataResourceWriter.AddResource("TestString", resourceManager["TestString"]);
			dataResourceWriter.AddResource("TestInteger", resourceManager["TestInteger", typeof(System.Int32)]);
			dataResourceWriter.AddResource("TestIcon", resourceManager["TestIcon", typeof(Icon)]);
			dataResourceWriter.AddResource("TestImage", resourceManager["TestImage", typeof(Bitmap)]);
			UnmanagedMemoryStream ums = (UnmanagedMemoryStream)resourceManager["TestSound", typeof(UnmanagedMemoryStream)];
			byte[] buffer = new byte[ums.Length];
			ums.Read(buffer, 0, (int)ums.Length);
			dataResourceWriter.AddResource("TestSound", buffer);
			dataResourceWriter.AddResource("TestColour", resourceManager["TestColour", typeof(Color)]);
			resourceDataNode = new ResourceDataNode("TestComment", resourceManager["TestComment"]);
			resourceDataNode.Comment = "Test comment";
			dataResourceWriter.AddResource(resourceDataNode.Name, resourceDataNode);
			resourceFileRef = new ResourceFileRef("test.bmp", typeof(Bitmap).AssemblyQualifiedName);
			resourceDataNode = new ResourceDataNode("TestFileRef", resourceFileRef);
			dataResourceWriter.AddResource(resourceDataNode.Name, resourceDataNode);
			dataResourceWriter.Close();

			resourceManager.CultureInfo = new CultureInfo("fr");
			dataResourceWriter = new DataResourceWriter(database, baseName, new CultureInfo("fr"));
			dataResourceWriter.AddResource("TestString", resourceManager["TestString"]);
			dataResourceWriter.AddResource("TestInteger", resourceManager["TestInteger", typeof(System.Int32)]);
			dataResourceWriter.AddResource("TestImage", resourceManager["TestImage", typeof(Bitmap)]);
			dataResourceWriter.Close();

			resourceManager.CultureInfo = new CultureInfo("de");
			dataResourceWriter = new DataResourceWriter(database, baseName, new CultureInfo("de"));
			dataResourceWriter.AddResource("TestString", resourceManager["TestString"]);
			dataResourceWriter.AddResource("TestInteger", resourceManager["TestInteger", typeof(System.Int32)]);
			dataResourceWriter.AddResource("TestImage", resourceManager["TestImage", typeof(Bitmap)]);
			dataResourceWriter.Close();
		}
		#endregion
	}
}
